package com.yesep.learn.jwt.demo.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.yesep.learn.jwt.demo.model.User;
import com.yesep.learn.jwt.demo.service.UserService;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
	private Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	private UserService srv;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		log.debug("表单登录用户名:" + username);
		return buildUser(username);
	}

	private UserDetails buildUser(String username) {
		User user = srv.get(username);
		UserDetails ud = null;
		if (null != user) {
			ud = new UserDetailsImpl(user);
		}
		return ud;
	}
}
